package com.jphotomanager.thread;

import java.awt.image.BufferedImage;
import java.io.File;

import org.apache.log4j.Logger;

import com.jphotomanager.io.ImageImporter;
import com.jphotomanager.io.ImageLibrary;
import com.mortennobel.imagescaling.ResampleOp;
import com.openroom.db.DBHelper;
import com.openroom.editing.JPEGImageProcessor;
import com.openroom.model.BackupMaster;
import com.openroom.model.ImageHolder;
import com.openroom.model.ModelLocator;
import com.openroom.model.Photograph;

public class ImageRotationTask implements Runnable {

    private static final Logger LOGGER = Logger
            .getLogger(ImageRotationTask.class);

    private Photograph photograph;
    private ROTATION rotation;

    private BufferedImage originalImage;

    private BufferedImage previewImage;

    private BufferedImage tnailImage;

    public static enum ROTATION {
        LEFT, RIGHT, FLIP_HORIZONTAL, FLIP_VERTICAL
    }

    public ImageRotationTask(Photograph photograph, ROTATION rotate) {
        super();
        this.photograph = photograph;
        this.rotation = rotate;
        System.out.println("ImageRotationTask.enclosing_method()");
    }

    @Override
    public void run() {
        try {
            LOGGER.info("trying to lock ow");
            ImageLibrary.getInstance().ow.lock();
            LOGGER.info("Locked ow");
            ModelLocator instance = ModelLocator.getInstance();
            JPEGImageProcessor ip2 = instance.getOrLoadImageProcessor();
            switch (this.rotation) {
            case LEFT:
                ip2 = ip2.rotateLeft();
                break;
            case RIGHT:
                ip2 = ip2.rotateRight();
                break;
            case FLIP_HORIZONTAL:
                ip2 = ip2.flipHorizontal();
                break;
            case FLIP_VERTICAL:
                ip2 = ip2.flipVertical();
                break;
            default:
                break;
            }

            originalImage = ip2.createImage();
            LOGGER.info("w=" + originalImage.getWidth());
            previewImage = createScaledImage(originalImage,
                    ImageImporter.PREVIEW_WIDTH, ImageImporter.PREVIEW_HEIGHT);

            tnailImage = createScaledImage(previewImage,
                    ImageImporter.PREVIEW_WIDTH, ImageImporter.PREVIEW_HEIGHT);
            ImageHolder imageHolder = new ImageHolder();
            imageHolder.setPreviewImage(previewImage);
            imageHolder.setThumbnailImage(tnailImage);
            ImageLibrary.updateCurrentImage(photograph, originalImage,
                    previewImage, tnailImage);
        } finally {
            ImageLibrary.getInstance().ow.unlock();
        }
    }

    public BufferedImage createScaledImage(BufferedImage read, int destWidth,
            int destHeight) {
        int origWidth = read.getWidth();
        int origHeight = read.getHeight();
        float aspectRatio = ((float) origWidth / origHeight);
        int scaledWidth = origWidth;
        int scaledHeight = origHeight;
        if (aspectRatio < 1) {
            scaledHeight = destHeight;
            scaledWidth = (int) (aspectRatio * destHeight);
        } else {
            scaledWidth = destWidth;
            scaledHeight = (int) (destWidth / aspectRatio);
        }
        ResampleOp resampleOp = new ResampleOp(scaledWidth, scaledHeight);
        BufferedImage rescaledImage = resampleOp.filter(read, null);
        return rescaledImage;
    }
}
